package telsoft.app.form;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;

import telsoft.app.util.*;
import telsoft.swing.*;
import telsoft.util.*;

/**
 *
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2009</p>
 *
 * <p>Company: Telsoft</p>
 *
 * @author HuongNV
 * @version 1.0
 */

public class PanelCommonLOV extends JPanel implements KeyListener
{
    public JXText txtCode = new JXText();
    public JXText txtName = new JXText();
    protected GridBagLayout gridBagLayout1 = new GridBagLayout();
    /* khai bao cac bien cua lop */
    protected DialogLOV mdlgLOV;
    protected boolean mblnEnfDisplay = true; //neu chi co 1 ban ghi co bat buoc phai hien thi lov ko
    private transient Vector mvtLOVActionListeners;
    private Vector mvtSelectedRow = null; // vector chua thong tin ban ghi dc chon (co them thong tin an)
    protected boolean mblnShowLov = false;
    protected boolean mblnForceShowLov = true; //default
    protected boolean mblnGetValueFromLOV = false; //default
    private Component mfrmParent;
    /* ham khoi tao*/
    private int miDisplayIndex = 1;
    private int miValueIndex = 0;

    public PanelCommonLOV(Component pfrmParent)
    {
        mfrmParent = pfrmParent;
        try
        {
            jbInit();
        }
        catch(Exception e)
        {
            e.printStackTrace();
            MessageBox.showMessageDialog(mfrmParent, e, Global.APP_NAME, MessageBox.ERROR_MESSAGE);
        }
    }

    public PanelCommonLOV()
    {
        try
        {
            jbInit();
	    mdlgLOV = new DialogLOV(this,new Vector(),new Vector());
        }
        catch(Exception e)
        {
            e.printStackTrace();
            MessageBox.showMessageDialog(mfrmParent, e, Global.APP_NAME, MessageBox.ERROR_MESSAGE);
        }
    }

    public PanelCommonLOV(Component pfrmParent, Vector pvtColName, String pstrQuery)
    {
        this(pfrmParent);
        mdlgLOV = new DialogLOV(pfrmParent, pvtColName, pstrQuery);
    }

    // Add by HungPT 24/11/2005
    public PanelCommonLOV(Component pfrmParent, String pstrQuery)
    {
        this(pfrmParent);
        Vector vtColName = new Vector();
        vtColName.add("Mã");
        vtColName.add("Tên");
        mdlgLOV = new DialogLOV(pfrmParent, vtColName, pstrQuery);
    }

    public PanelCommonLOV(Component pfrmParent, String pstrCode, String pstrName, String pstrQuery)
    {
        this(pfrmParent);
        Vector vtColName = new Vector();
        vtColName.add(pstrCode);
        vtColName.add(pstrName);
        mdlgLOV = new DialogLOV(pfrmParent, vtColName, pstrQuery);
    }

    public PanelCommonLOV(Component pfrmParent, String pstrCode, String pstrName)
    {
        this(pfrmParent);
        Vector vtColName = new Vector();
        vtColName.add(pstrCode);
        vtColName.add(pstrName);
        mdlgLOV = new DialogLOV(pfrmParent, vtColName, "");
    }

    public void setTitleForDialog(String pstrTitle)
    {
        mdlgLOV.setTitle(pstrTitle);
    }

    public PanelCommonLOV(JFrame pfrmParent, Vector pvtColName, String pstrQuery)
    {
        this(pfrmParent);
        mdlgLOV = new DialogLOV(pfrmParent, pvtColName, pstrQuery);
    }

    public PanelCommonLOV(Component pfrmParent, Vector pvtColName, Vector pvtLovData)
    {
        this(pfrmParent);
        mdlgLOV = new DialogLOV(pfrmParent, pvtColName, pvtLovData);
    }

    public PanelCommonLOV(JFrame pfrmParent, Vector pvtColName, Vector pvtLovData)
    {
        this(pfrmParent);
        mdlgLOV = new DialogLOV(pfrmParent, pvtColName, pvtLovData);
    }

    public void setWaitForData(boolean bWait)
    {
        mdlgLOV.bDataLoaded = !bWait;
    }

    public void setCommonLov(Component pfrmParent, Vector pvtColName, Vector pvtLovData)
    {
        mfrmParent = pfrmParent;
        mdlgLOV = new DialogLOV(pfrmParent, pvtColName, pvtLovData);
    }

    public void setCommonLov(Vector pvtColName, Vector pvtLovData)
    {
        mdlgLOV = new DialogLOV(mfrmParent, pvtColName, pvtLovData);
    }

    private void jbInit() throws Exception
    {
        txtCode.setDocument(new JUpperCaseDocument());
        this.setLayout(gridBagLayout1);
        txtCode.setMinimumSize(new Dimension(100, 20));
        txtCode.setPreferredSize(new Dimension(100, 20));
        txtCode.setFilter(":;<=>?@[\\]^'`");
        txtCode.setText("");
        txtName.setEnabled(false);
        txtName.setBackground(Color.WHITE);
        this.add(txtCode, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST,
            GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
        this.add(txtName, new GridBagConstraints(1, 0, GridBagConstraints.REMAINDER, 1, 4.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
        //add listener
        txtCode.addFocusListener(new java.awt.event.FocusAdapter()
        {
            public void focusGained(FocusEvent e)
            {
                try
                {
                    if(e.isTemporary())
                    {
                        return;
                    }

                    txtCode_focusGained(e);
                }
                catch(Exception ex)
                {
                    ex.printStackTrace();
                    MessageBox.showMessageDialog(mfrmParent, ex, Global.APP_NAME,
                        MessageBox.ERROR_MESSAGE);
                }
            }

            public void focusLost(FocusEvent e)
            {
                try
                {
                    if(e.isTemporary())
                    {
                        return;
                    }

                    txtCode_focusLost(e);
                }
                catch(Exception ex)
                {
                    ex.printStackTrace();
                    MessageBox.showMessageDialog(mfrmParent, ex, Global.APP_NAME,
                        MessageBox.ERROR_MESSAGE);
                }
            }
        });
        txtCode.addKeyListener(this);
        Skin.applySkin(this);
    }

    //cac ham giao tiep voi ben ngoai
    public void setQuery(String pstrQuery) throws AppException
    {
        mdlgLOV.setQuery(pstrQuery);
    }

    public String getCode()
    {
        return txtCode.getText().trim();
    }
    public JXText getTxtCode()
   {
       return txtCode;
   }

    public void setCode(String pstrCode)
    {
        setCode(pstrCode, true);
    }

    public void setCode(String pstrCode, boolean bWait)
    {
        try
        {
            int iTimeOut = 10000;
            int iCurrentSleep = 0;
            while(!mdlgLOV.bDataLoaded && !pstrCode.equals("") && bWait && iCurrentSleep <= iTimeOut)
            {
                try
                {
                    iCurrentSleep += 50;
                    Thread.currentThread().sleep(50);
                }
                catch(InterruptedException ex1)
                {
                    ex1.printStackTrace();
                }
            }
            txtCode_focusGained(null);
            txtCode.setText(pstrCode);
            txtCode_focusLost(null);
        }
        catch(AppException ex)
        {
            ex.printStackTrace();
            MessageBox.showMessageDialog(mfrmParent, ex, Global.APP_NAME, MessageBox.ERROR_MESSAGE);
        }
    }

    public void setCode(String pstrCode, boolean bWait, boolean focus)
    {
        try
        {
            int iTimeOut = 10000;
            int iCurrentSleep = 0;
            while(!mdlgLOV.bDataLoaded && !pstrCode.equals("") && bWait && iCurrentSleep <= iTimeOut)
            {
                try
                {
                    iCurrentSleep += 50;
                    Thread.currentThread().sleep(50);
                }
                catch(InterruptedException ex1)
                {
                    ex1.printStackTrace();
                }
            }
            if(focus)
            {
                txtCode_focusGained(null);
                txtCode.setText(pstrCode);
                txtCode_focusLost(null);
            }
            else
            {
                txtCode.setText(pstrCode);
            }
        }
        catch(AppException ex)
        {
            ex.printStackTrace();
            MessageBox.showMessageDialog(mfrmParent, ex, Global.APP_NAME, MessageBox.ERROR_MESSAGE);
        }
    }

    public String getName()
    {
        return txtName.getText().trim();
    }

    public Vector getData()
    {
        return mvtSelectedRow;
    }

    public Vector getLOVData()
    {
        return mdlgLOV.getLOVData();
    }

    public void setLOVData(Vector pvtLOVData)
    {
        mdlgLOV.setLOVData(pvtLOVData);
    }

    //cac ham su kien
    void txtCode_focusGained(FocusEvent e) throws AppException
    {
        if(!mblnGetValueFromLOV)
        {
            txtCode.backup();
        }
        mblnGetValueFromLOV = false;
        mvtSelectedRow = null;
        txtCode.setSelectionStart(0);
        txtCode.setSelectionEnd(txtCode.getText().trim().length());
        fireLOVAction(new LOVActionEvent(this, LOVActionEvent.ACTION_FOCUS_GAINED));
    }

    public void txtCode_focusLost(FocusEvent e) throws AppException
    {
        //txtCode.backup();
        mblnEnfDisplay = false;
        showLov();
        mblnEnfDisplay = true;
        mblnGetValueFromLOV = false;
        fireLOVAction(new LOVActionEvent(this, LOVActionEvent.ACTION_FOCUS_LOST));
    }

    protected void fireLOVAction(LOVActionEvent e) throws AppException
    {
        if(mvtLOVActionListeners != null)
        {
            Vector listeners = mvtLOVActionListeners;
            int count = listeners.size();
            for(int i = 0;i < count;i++)
            {
                ((LOVActionListener)listeners.elementAt(i)).LOVAction(e);
            }
        }
    }

    public synchronized void removeLOVActionListener(LOVActionListener l)
    {
        if(mvtLOVActionListeners != null && mvtLOVActionListeners.contains(l))
        {
            Vector v = (Vector)mvtLOVActionListeners.clone();
            v.removeElement(l);
            mvtLOVActionListeners = v;
        }
    }

    public synchronized void addLOVActionListener(LOVActionListener l)
    {
        Vector v = mvtLOVActionListeners == null ? new Vector(2) : (Vector)mvtLOVActionListeners.clone();
        if(!v.contains(l))
        {
            v.addElement(l);
            mvtLOVActionListeners = v;
        }
    }

    public void setNameVisible(boolean pblnVisible)
    {
        txtName.setVisible(pblnVisible);
    }

    public void setFilter(String pstrFilter)
    {
        mdlgLOV.setFilter(pstrFilter);
    }

    public void requestFocus()
    {
        txtCode.requestFocus();
    }

    public void keyPressed(KeyEvent e)
    {
        if(txtCode.isEditable() && ((e.isControlDown() && e.getKeyCode() == KeyEvent.VK_L)
            || (e.getKeyCode() == KeyEvent.VK_F9)))
        {
            boolean vblnShowLov = mblnShowLov;
            boolean vblnForceShowLov = mblnForceShowLov;
            mblnShowLov = true; //bat buoc phai hien thi lov
            mblnForceShowLov = true;
            txtCode.backup();
            showLov();
            if(mvtSelectedRow == null || mvtSelectedRow.size() == 0)
            {
                txtCode.restore();
            }
            mblnShowLov = vblnShowLov;
            mblnForceShowLov = vblnForceShowLov;
        }
    }

    public void keyReleased(KeyEvent ke)
    {
    }

    public void keyTyped(KeyEvent e)
    {
    }

    private void showLov()
    {
        if(!txtCode.getText().trim().equals("") && (mdlgLOV.getLOVData() == null ||
            mdlgLOV.getLOVData().size() <= 0) && mblnForceShowLov)
        {
            MessageBox.showMessageDialog(mfrmParent,
                "LOVIsEmpty" + (String)mdlgLOV.getLOVColumnName().elementAt(0), Global.APP_NAME,
                MessageBox.ERROR_MESSAGE);
            txtCode.setText("");
            return;
        }
        if(mblnShowLov || !txtCode.getText().trim().equals(""))
        {
            refressCurrentValue(mblnEnfDisplay, mblnForceShowLov);
        }
        if(mvtSelectedRow == null && !txtCode.getText().trim().equals("") && mblnForceShowLov)
        {
            txtCode.requestFocus();
        }
        ////////////////////////////////////////////////////////////
        //add by ThinhPQ
        if(txtCode.getText().trim().equals(""))
        {
            txtName.setText("");
        }
        ////////////////////////////////////////////////////////////
    }

    public void setShowLov(boolean pblnShowLov)
    {
        mblnShowLov = pblnShowLov;
    }

    public void setForceShowLov(boolean blnForceShowLov)
    {
        mblnForceShowLov = blnForceShowLov;
    }

    public void setColumeName(Vector vtColumeName) throws AppException
    {
        try
        {
            mdlgLOV.languageColumn(vtColumeName);
            mdlgLOV.updateLanguage();
        }
        catch(AppException ex)
        {
            throw ex;
        }
    }

    public void setColumeName(String pstrCode, String pstrName) throws AppException
    {
        Vector vtColName = new Vector();
        vtColName.add(pstrCode);
        vtColName.add(pstrName);
        setColumeName(vtColName);

    }

    /**
     * can call from out outside of this object
     * @author: TrungDD
     * @since: 07/12/2005
     */
    public void refressCurrentValue()
    {
        refressCurrentValue(false, false);
    }

    public void refressCurrentValue(boolean blnEnfDisplay, boolean bForceShowLov)
    {
        mvtSelectedRow = mdlgLOV.showLOV(txtCode.getText().trim(), blnEnfDisplay, bForceShowLov);
        if(mvtSelectedRow != null)
        {
            txtCode.setText((String)mvtSelectedRow.elementAt(miValueIndex));
            txtName.setText((String)mvtSelectedRow.elementAt(miDisplayIndex));
            txtName.setCaretPosition(0);
            mblnGetValueFromLOV = true;
        }
    }



    public void clearText()
    {
        txtCode.setText("");
        txtName.setText("");
    }

    public void setValue(String strValue)
    {
        setValue(strValue, true);
    }

    public void setValue(String strValue, boolean bWait)
    {
        try
        {
            int iTimeOut = 10000;
            int iCurrentSleep = 0;
            while(!mdlgLOV.bDataLoaded && !strValue.equals("") && bWait && iCurrentSleep <= iTimeOut)
            {
                try
                {
                    iCurrentSleep += 50;
                    Thread.currentThread().sleep(50);
                }
                catch(InterruptedException ex1)
                {
                    ex1.printStackTrace();
                }
            }
            //Lay Code
            Vector vtLOVData = mdlgLOV.getLOVData();
            String strCode = "";
            for(int i = 0;i < vtLOVData.size();i++)
            {
                Vector vtElement = ((Vector)vtLOVData.elementAt(i));
                if(vtElement.elementAt(miValueIndex).toString().equals(strValue))
                {
                    strCode = vtElement.elementAt(0).toString();
                }
            }
            txtCode_focusGained(null);
            txtCode.setText(strCode);
            txtCode_focusLost(null);
        }
        catch(AppException ex)
        {
            ex.printStackTrace();
            MessageBox.showMessageDialog(mfrmParent, ex, Global.APP_NAME, MessageBox.ERROR_MESSAGE);
        }
    }

    public void setValueIndex(int iValueIndex)
    {
        miValueIndex = iValueIndex;
    }

    public int getValueIndex()
    {
        return miValueIndex;
    }
}
